/*
 * Copyright 2008 CSC Consulting Group under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in 
 * compliance with the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License.
 */
package com.waw.calendar.plugin.action;

import java.util.ArrayList;
import java.util.Iterator;

import com.jivesoftware.community.Community;
import com.jivesoftware.community.CommunityManager;
import com.waw.calendar.plugin.CalendarConsts;
import com.waw.calendar.plugin.PluginUtils;

/**
 * This action class supports the configuration of individual user Google Calendars. 
 * To support that configuration, the action provides a method for returning the 
 * calendars of communities within the system. 
 * These are communities that have an extended property (CalendarConsts.COMM_CALENDAR_URL).
 *
 * @author jtremlett
 */
public class ConfigureCalendarAction extends BaseCalendarAction {

	private static final long serialVersionUID = 1L;

	/**
	 * Always return success.
	 * @see com.jivesoftware.community.action.ViewProfile#execute()
	 */
	public String execute() {
        super.execute();
        
        return SUCCESS;
    }
	
	/**
	 * Return the list of Communities with associate Group Calendars.
	 * @see GroupCalendar
	 * @see #recurseComm(Community, ArrayList)
	 * @return an Iterator of GroupCalendar JavaBeans holding the group calendars defined in the system.
	 */
	public Iterator<GroupCalendar> getGroupCalendars()
	{
		ArrayList<GroupCalendar> groupsWithCals = new ArrayList<GroupCalendar>();
		CommunityManager cMan = PluginUtils.getCommunityManager();
		Community root = cMan.getRootCommunity();
		recurseComm(root, groupsWithCals);
		return groupsWithCals.iterator();
	}

	/**
	 * This method recurses down the Community hierarchy looking for Communities with 
	 * Google Calendars associated with them.  
	 * A recursive search is necessary as the GroupManager has a root Community getter 
	 * and no iterator function over the Communities.
	 * @param comm the Community to recursively search
	 * @param groupsWithCals The list of communities with Google Calendars. 
	 */
	private void recurseComm(Community comm, ArrayList<GroupCalendar> groupsWithCals) 
	{
		// check for URL
		String url = comm.getProperties().get(CalendarConsts.COMM_CALENDAR_URL);
		if(url != null && url.length() > 0)
		{
			String name = comm.getName();
			GroupCalendar gCal = new GroupCalendar();
			gCal.setGroupName(name);
			gCal.setCalendarURL(url);
			groupsWithCals.add(gCal);
		}
		
		// recurse over children
		Iterator<Community> childIter = comm.getCommunities();
		if(!childIter.hasNext())return;
		while (childIter.hasNext())
		{
			Community child = childIter.next();
			recurseComm(child, groupsWithCals);
		}
	}
}
